package com.castle.fortress.admin.temp.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.temp.entity.TempTreeEntity;
import com.castle.fortress.admin.temp.dto.TempTreeDto;
import com.castle.fortress.admin.temp.mapper.TempTreeMapper;
import com.castle.fortress.admin.temp.service.TempTreeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * 生成树示例 服务实现类
 *
 * @author 
 * @since 2023-08-03
 */
@Service
public class TempTreeServiceImpl extends ServiceImpl<TempTreeMapper, TempTreeEntity> implements TempTreeService {
    /**
    * 获取查询条件
    * @param tempTreeDto
    * @return
    */
    public QueryWrapper<TempTreeEntity> getWrapper(TempTreeDto tempTreeDto){
        QueryWrapper<TempTreeEntity> wrapper= new QueryWrapper();
        if(tempTreeDto != null){
            TempTreeEntity tempTreeEntity = ConvertUtil.transformObj(tempTreeDto,TempTreeEntity.class);
            wrapper.like(StrUtil.isNotEmpty(tempTreeEntity.getName()),"name",tempTreeEntity.getName());
        }
        return wrapper;
    }


    @Override
    public IPage<TempTreeDto> pageTempTree(Page<TempTreeDto> page, TempTreeDto tempTreeDto) {
        QueryWrapper<TempTreeEntity> wrapper = getWrapper(tempTreeDto);
        Page<TempTreeEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<TempTreeEntity> tempTreePage=baseMapper.selectPage(pageEntity,wrapper);
        Page<TempTreeDto> pageDto = new Page(tempTreePage.getCurrent(), tempTreePage.getSize(),tempTreePage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(tempTreePage.getRecords(),TempTreeDto.class));
        return pageDto;
    }


    @Override
    public List<TempTreeDto> listTempTree(TempTreeDto tempTreeDto){
        QueryWrapper<TempTreeEntity> wrapper = getWrapper(tempTreeDto);
        List<TempTreeEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,TempTreeDto.class);
    }
}

